﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Model;
using V5_Common;
using V5_BLL;
using System.Data;

namespace V5_EShop._Template.Default
{
    public partial class product_answer : BasicPage
    {
        public ModelProduct product = new ModelProduct();
        int productid = 0, askid = 0;
        public ModelProductAsk m_ask = new ModelProductAsk();
        public BLLUser b_user = new BLLUser();
        public BLLUser m_user = new BLLUser();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.check_member_login())
            {
                Response.Redirect("/login.aspx?returnurl=" + HTTPS.GetCurrentUrl());
            }
            productid = Input.SetInt(Request["productid"]);
            askid = Input.SetInt(Request["askid"]);
            if (askid > 0)
            {
                m_ask = new BLLProductAsk().GetModel(askid);
            }
            product = new BLLProduct().GetModel(productid);
            this.hfproductid.Value = productid.ToString();
            this.hfaskid.Value = askid.ToString();
            if (!this.IsPostBack)
            {
                askid = Input.SetInt(Request["askid"]);
                if (askid > 0)
                {
                    m_ask = new BLLProductAsk().GetModel(askid);
                }
                this.Bind_List();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            base.OnInit(e);
        }


        /// <summary>
        /// 绑定回答列表
        /// </summary>
        private void Bind_List()
        {
            string strWhere = "";
            int Count = 0;
            strWhere = " productid=" + Utils.FilterInjection(productid) + " and askid=" + Utils.FilterInjection(askid) + " and Passed=1 ";
            DataTable dt = new BLLProductAnswer().GetList(Pager.PageSize, Pager.CurrentPageIndex, strWhere, ref Count).Tables[0];
            this.Pager.RecordCount = Count;
            this.rpList.DataSource = dt;
            this.rpList.DataBind();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            int m_productid = Input.SetInt(this.hfproductid.Value);
            int m_askid = Input.SetInt(this.hfaskid.Value);
            string AnswerContent = Input.SetString(this.txtanswerContent.Text);
            if (AnswerContent.Trim() == "")
            {
                Response.Redirect(HTTPS.ComeUrl);
            }
            if (m_productid == 0 && m_askid == 0)
            {
                Pr.RegClientAlertAndRedirect(this, "对不起!参数有误不能提交!", HTTPS.ComeUrl);
                return;
            }
            ModelProductAnswer model = new ModelProductAnswer();
            BLLProductAnswer bll = new BLLProductAnswer();
            model.AnswerContent = AnswerContent;
            model.AskID = m_askid;
            model.MemberID = user.ID;
            model.ProductID = m_productid;
            model.AddDateTime = DateTime.Now;
            model.Passed = 0;
            model.QuestionID = 0;
            model.GetIP = HTTPS.GetIP;
            bll.Add(model);
            Pr.RegClientAlertAndRedirect(this, "恭喜您!您的回答已经成功!请等待管理员的审核!", "/product-answer.aspx?productid=" + m_productid + "&askid=" + m_askid);
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_List();
        }
    }
}
